#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QString>
#include <QFile>
#include <QFileDialog>
#include <QThread>
#include <QDebug>

#include <QSerialPort>
#include <QSerialPortInfo>

#include "commserialsingle.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    m_FileName(""),
    m_FileReadFlag(false),
    m_SerialOpenFlag(false)
{
    ui->setupUi(this);
    SetupUI();
    SetupConnect();

    InitSerial();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::SetupUI()
{
    ui->centralWidget->setAutoFillBackground(false);

    ui->MainTab->setTabText(0, QStringLiteral("火焰仿真"));
    ui->MainTab->setTabText(1, QStringLiteral("串口收发"));

    //tab0 FireSimulation
    ui->FilelineEdit->setFixedHeight(25);

    ui->FileRadioGroup->setId(ui->TxtRadio, 0);
    ui->FileRadioGroup->setId(ui->ExcelRadio, 1);
    ui->ChannelRadioGroup->setId(ui->ThreeRadio, 0);
    ui->ChannelRadioGroup->setId(ui->SingleRadio, 1);
    ui->TxtRadio->setChecked(true);
    ui->ThreeRadio->setChecked(true);

    //tab1 Serial
    ui->WavelineEdit->setFixedWidth(40);
}

void MainWindow::SetupConnect()
{
    //ui btn is already connected to slot

    connect(ui->FileRadioGroup, SIGNAL(buttonClicked(int)), this, SLOT(FileRadioGroup_clicked(int)));
    connect(ui->ChannelRadioGroup, SIGNAL(buttonClicked(int)), this, SLOT(ChannelRadioGroup_clicked(int)));
}

void MainWindow::InitSerial()
{
    for(auto &dev : QSerialPortInfo::availablePorts())
    {
        QSerialPort com;
        com.setPort(dev);
        if(com.open(QIODevice::ReadWrite)) {
            ui->SerialCBox->addItem(com.portName());
            com.close();
        }
    }
}

//slot functions
void MainWindow::on_FileBtn_clicked()
{
    QString mf = (ui->FileRadioGroup->checkedId() == 0) ? tr("Image Files (*.txt)") : tr("Image Files (*.xls, *.xlsx)");

    m_FileName = QFileDialog::getOpenFileName(this, tr("Open Image"), "./", mf);
    if(m_FileName.isEmpty()) return;

    ui->FilelineEdit->setText(m_FileName);
    m_FileReadFlag = false;
}

void MainWindow::on_SimulateBtn_clicked()
{

}

void MainWindow::on_OriginBtn_clicked()
{

}

void MainWindow::on_MovingFBtn_clicked()
{

}

void MainWindow::on_MovingFAttrBtn_clicked()
{

}

void MainWindow::on_SerialBtn_clicked()
{
    if(!m_SerialOpenFlag) {
        ui->SerialBtn->setText(QStringLiteral("关闭串口"));

        QString name = ui->SerialCBox->currentText();
        CommSerialSingle::GetInstance()->OpenPort(name);
    }
    else {
        ui->SerialBtn->setText(QStringLiteral("打开串口"));
        CommSerialSingle::GetInstance()->ClosePort();
    }

    m_SerialOpenFlag = !m_SerialOpenFlag;
}

void MainWindow::on_RefreshSerialBtn_clicked()
{
    ui->SerialCBox->clear();
    InitSerial();
}

void MainWindow::on_CalibZeroBtn_clicked()
{

}

void MainWindow::on_WaveBtn_clicked()
{

}

void MainWindow::on_UsrCmdBtn_clicked()
{

}

void MainWindow::on_SerialDataRevBtn_clicked()
{

}

void MainWindow::on_DataSaveBtn_clicked()
{

}

void MainWindow::on_DataClrBtn_clicked()
{

}

void MainWindow::FileRadioGroup_clicked(int flag)
{
    qDebug() << "fileRadio clicked " << flag;
}

void MainWindow::ChannelRadioGroup_clicked(int flag)
{
    qDebug() << "channelRadio clicked " << flag;
}
